package cn.easytogo.action.admin;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.easytogo.action.BaseAction;
import cn.easytogo.bean.Resource;
import cn.easytogo.bean.Role;
import cn.easytogo.bean.Usergroup;
import cn.easytogo.service.IRoleDAO;
import cn.easytogo.service.IUsergroupDAO;

public class UsergroupAction  extends BaseAction{
	private IUsergroupDAO usergroupDAO;
	private IRoleDAO roleDAO;
	private String name;
	private String id;
	private List usergroupList;
	private List roleList;
	private String roleIds;
	/**
	 * 前往用户组列表维护页面
	 * */
	public String toList(){
		Map map = new HashMap();
		try {
			usergroupList = usergroupDAO.selectUsergroupsWithoutRolesByMap(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "to_list";
	}
	/**
	 * 前往用户组新加页面
	 * */
	public String toNew(){
		initRoleList();
		return "to_new";
	}
	/**
	 * 保存用户组
	 * */
	public void saveNew(){
		Usergroup ug = new Usergroup();
		ug.setName(name);
		ug.setRoleIds(roleIds);
		ug.setUsergroup_flag("1");
		try {
			usergroupDAO.insertUsergroup(ug);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			print("failed");
		}
	}
	/**
	 * 前往用户组修改页面
	 * */
	public String toUpdate(){
		initRoleList();
		Map map = new HashMap();
		map.put("id", id);
		try {
			List l = usergroupDAO.selectUsergroupsWithoutRolesByMap(map);
			if(l!=null&&l.size()>0){
				Usergroup usergroup = (Usergroup) l.get(0);
				name = usergroup.getName();
				roleIds = usergroup.getRoleIds();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "to_new";
	}
	/**
	 * 更新用户组
	 * */
	public void saveUpdate(){
		if(id!=null&&!"".equals(id)){
			Usergroup usergroup = new Usergroup();
			usergroup.setId(id);
			usergroup.setName(name);
			usergroup.setRoleIds(roleIds);
			usergroup.setUsergroup_flag("1");
			try {
				usergroupDAO.updateUsergroup(usergroup);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	/**
	 * 删除用户组
	 * */
	public void delete(){
		if(id!=null&&!"".equals(id)){
			Usergroup usergroup = new Usergroup();
			usergroup.setId(id);
			usergroup.setUsergroup_flag("1");
			try {
				usergroupDAO.deleteUsergroup(usergroup);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				print("failed");
				return;
			}
		}
	}
	/**
	 * 初始化角色List
	 * */
	private void initRoleList(){
		Map map = new HashMap();
		try {
			roleList = roleDAO.selectRolesByMap(map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public IUsergroupDAO getUsergroupDAO() {
		return usergroupDAO;
	}
	public void setUsergroupDAO(IUsergroupDAO usergroupDAO) {
		this.usergroupDAO = usergroupDAO;
	}
	public IRoleDAO getRoleDAO() {
		return roleDAO;
	}
	public void setRoleDAO(IRoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List getUsergroupList() {
		return usergroupList;
	}
	public void setUsergroupList(List usergroupList) {
		this.usergroupList = usergroupList;
	}
	public List getRoleList() {
		return roleList;
	}
	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}
	public String getRoleIds() {
		return roleIds;
	}
	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}
	
}
